package com.health.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.health.constant.Constants;

public class LoginFilter implements Filter
{

	Log	log	= LogFactory.getLog(LoginFilter.class);

	/**
	 * check the session is NULL?when visit the ***.jsp(in web.xml)
	 */
	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException
	{
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		HttpSession session = request.getSession();
		if (null != session.getAttribute(Constants.LOGININFO))
		{
			chain.doFilter(request, response);
		}
		else
		{
			log.info("LoginFilter filtering a request");
			response.sendRedirect("user-reg-login.html");
		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException
	{
		System.out.println("---------LoginFilter inited--------");
	}

	@Override
	public void destroy()
	{
		System.out.println("---------LoginFilter destroyed--------");
	}

}
